<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/8/14
 * Time: 15:33
 */

namespace app\api\controller\v1;


use app\api\service\AppToken;
use app\api\service\BossToken;
use app\api\service\EnterpriseToken;
use app\api\service\UserToken;
use app\api\validate\AppTokenGet;
use app\api\validate\TokenGet;
use app\lib\exception\ParameterException;
use think\Exception;
use \app\api\service\Token as TokenService;

class Token
{
    /**
     * 获取token
     * @param string $code
     * @param string $id
     * @return \think\response\Json
     * @throws Exception
     */
    public function getToken($code = '', $id = ''){
        (new TokenGet())->goCheck();
        $userToken = new UserToken($code,$id);
        $token = $userToken->get();
        return json([
            'token'=>$token[0],
            'auth'=>$token[1],
            'time'=>$token[2],
            'user'=>$token[3]
        ]);
    }

    /**
     * 获取第三方token
     * @param string $ac
     * @param string $se
     * @return \think\response\Json
     * @throws Exception
     * @throws \app\lib\exception\TokenException
     */
    public function getAppToken($ac='',$se=''){
        (new AppTokenGet())->goCheck();
        $appToken = new AppToken();
        $token =  $appToken->getToken($ac,$se);
        return json([
            'msg'=>'登录成功',
            'error_code'=>0,
            'token'=>$token,
        ]);
    }

    /**
     * 新华扬CRM登陆获取用户信息
     * @param $code
     * @return false|string
     * @throws ParameterException
     * @throws \app\lib\exception\AuthException
     * @throws \app\lib\exception\TokenException
     */
    public function getEnterpriseToken($code){
            if ($code == null){
                throw new ParameterException([
                    'msg'=>'请求用户信息参数错误'
                ]);
            }
            $service = new EnterpriseToken();
            $info =  $service->getToken($code);
            return json_encode($info);
    }
    /**
     * 新华扬人事用户登录时，获取用户信息。不做访问限制
     * 获取用户信息
     * @param $code
     * @return mixed
     * @throws ParameterException
     */
    public function getBossToken($code){
        if ($code == null){
            throw new ParameterException([
                'msg'=>'请求用户信息参数错误'
            ]);
        }
        $service = new BossToken();
        $info =  $service->getToken($code);
        return json_encode($info);
    }

    /**
     * 根据ID获取token
     * @param string $ac
     * @param string $se
     * @return \think\response\Json
     */
    public function getAppTokenById($ac='',$se=''){
        (new AppTokenGet())->goCheck();
        $appToken = new AppToken();
        $token =  $appToken->getTokenById($ac);
        return json([
            'msg'=>'登录成功',
            'error_code'=>0,
            'token'=>$token,
        ]);
    }

    /**
     * 验证token
     * @param string $token
     * @return \think\response\Json
     * @throws Exception
     */
    public function verifyToken($token=''){
        if (!$token ){
            throw new Exception([
                'token不允许为空'
            ]);
        }
        $validate = TokenService::verifyToken($token);
        return json([
           'isValid'=>$validate
        ]);
    }
}